package android.cs2340UDT;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

/*
 * Class handles creating an item and then adding it to the user
 */
public class ListSettingsView extends Activity{

	Manager manage = Security.getManager();
	
	boolean startUp = true;

    EditText inputDueDateDay;
    EditText inputDueDateMonth;
    EditText inputDueDateYear;
    
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.list_settings);
    	
    	// These retrieve the text input boxes
      final EditText inputDueDateMonth = (EditText) findViewById(R.id.sortMonth);
      final EditText inputDueDateDay = (EditText) findViewById(R.id.sortDay);
      final EditText inputDueDateYear = (EditText) findViewById(R.id.sortYear);
      
      //int sortDate = Integer.parseInt(((inputDueDateMonth.toString()).concat(inputDueDateDay.toString()).concat(inputDueDateYear.toString())));
      
      //manage.getCurrentItemList().sortDate = sortDate;
      
      ImageButton backButton = (ImageButton) findViewById(R.id.ListBack);
      Button MapButton = (Button) findViewById(R.id.Map);
      
      final Spinner spinner = (Spinner) findViewById(R.id.sortSpinner);
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    		this, R.array.sorts_array, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      
      spinner.setOnItemSelectedListener( new OnItemSelectedListener(){
    	  
      	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
      		
      		if(!startUp) {
      			if(spinner.getSelectedItemPosition() == 0){
      				manage.getCurrentItemList().sortString = "";
      			}
      		
	      		if(spinner.getSelectedItemPosition() == 1){
	      			manage.getCurrentItemList().sortString = "All Items";
	      		}
	      		else if(spinner.getSelectedItemPosition() == 2){
	      			manage.getCurrentItemList().sortString = "Completed";
	      		}
	      		else if(spinner.getSelectedItemPosition() == 3){
	      			manage.getCurrentItemList().sortString = "Not Completed";
	      		}
	      		else if(spinner.getSelectedItemPosition() == 4){
	      			manage.getCurrentItemList().sortString = "Before Date";
	      		}
	      		else if(spinner.getSelectedItemPosition() == 5){
	      			manage.getCurrentItemList().sortString = "After Date";
	      		}
	      		
	      		//Starting a new Intent
	    		Intent nextScreen = new Intent(getApplicationContext(), ListViewActivity.class);
	    		finish();
	    		//Switches over to MainScreenActivity
	    		startActivity(nextScreen);
    		
      		}
      		else {
      			startUp = false;
      		}
      		
      	}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
      });
      
	    //This is when the user clicks the back button
	    backButton.setOnClickListener(new View.OnClickListener() {
	   	 
	        public void onClick(View arg0) {
	        	//Starting a new Intent
	    		Intent nextScreen = new Intent(getApplicationContext(), ListViewActivity.class);
	    		finish();
	    		//Switches over to MainScreenActivity
	    		startActivity(nextScreen);
	        }
	    });
	    
	    MapButton.setOnClickListener(new View.OnClickListener() {
	    	
	    	public void onClick(View arg0){
	    		Intent nextScreen = new Intent(getApplication(), MapScreenActivity.class);
	    		startActivity(nextScreen);
	    	}
	    });
      
    }
    
}
	
//	public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        //Displays the login screen
//        setContentView(R.layout.list_settings);
//        
//        // These retrieve the text input boxes
//        final EditText inputDueDateMonth = (EditText) findViewById(R.id.sortMonth);
//        final EditText inputDueDateDay = (EditText) findViewById(R.id.sortDay);
//        final EditText inputDueDateYear = (EditText) findViewById(R.id.sortYear);
//
//        Button saveSettingsButton = (Button) findViewById(R.id.createTaskButton);
//        
//        Spinner spinner = (Spinner) findViewById(R.id.sortSpinner);
//        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
//        		this, R.array.sorts_array, android.R.layout.simple_spinner_item);
//        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        spinner.setAdapter(adapter);
//        
//        //This is when the user clicks the create a task button
//        saveSettingsButton.setOnClickListener(new View.OnClickListener() {
//        	 
//            public void onClick(View arg0) {
//            	
//            	String month = inputDueDateMonth.getText().toString();
//            	String day = inputDueDateDay.getText().toString();
//            	String year = inputDueDateYear.getText().toString();
//            	
//            	int result = 0;
//            		
//            	if(result == 0) {
//	            	//adds all of the input and adds it to the item
//	            	
//	            	//Starting a new Intent
//	        		Intent nextScreen = new Intent(getApplicationContext(), ListViewActivity.class);
//	        		finish();
//	        		//Switches over to MainScreenActivity
//	        		startActivity(nextScreen);
//            	}
//            	else if(result == 1) {
//            		// due date error popup
//            	}
//            	else {
//            		// unknown error popup
//            	}
//            }
//        });
//        
//        ImageButton backButton = (ImageButton) findViewById(R.id.ListBack);
//        
//        //This is when the user clicks the back button
//        backButton.setOnClickListener(new View.OnClickListener() {
//       	 
//            public void onClick(View arg0) {
//            	//Starting a new Intent
//        		Intent nextScreen = new Intent(getApplicationContext(), ListViewActivity.class);
//        		finish();
//        		//Switches over to MainScreenActivity
//        		startActivity(nextScreen);
//            }
//        });
//        
//        
//        spinner.setOnItemSelectedListener( new OnItemSelectedListener(){
//        	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
//        		
//        		if(parent.getItemAtPosition(pos).toString() == "All Items"){
//        			manage.getCurrentItemList().sortString = "All Items";
//        		}
//        		else if(parent.getItemAtPosition(pos).toString() == "Completed"){
//        			manage.getCurrentItemList().sortString = "Completed";
//        		}
//        		else if(parent.getItemAtPosition(pos).toString() == "Not Completed"){
//        			manage.getCurrentItemList().sortString = "Not Completed";
//        		}
//        		else if(parent.getItemAtPosition(pos).toString() == "Before Date"){
//        			manage.getCurrentItemList().sortString = "Before Date";
//        		}
//        		else if(parent.getItemAtPosition(pos).toString() == "After Date"){
//        			manage.getCurrentItemList().sortString = "After Date";
//        		}
//        		
//        	}
//
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//        });
//    }
